import { _decorator, Component, Label, Node } from 'cc';
import { PalBattleEnemy, PalBattleEventEnum, PalBattleHero } from './PalBattleSystem';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;

@ccclass('HeroItem')
export class HeroItem extends Component {
    @property(Label)
    nameLabel: Label = null!;
    @property(Label)
    hpLabel: Label = null!;
    @property(Node)
    defenseNode: Node = null!;

    data: PalBattleHero;

    heroStatusChange(args: { UUID: string; status: string; value: any }) {
        const { UUID, status, value } = args;
        if (UUID !== this.data.saveData.UUID) return;
        this.data.saveData[status] = value;
        this.render();
        this.renderDead();
    }
    heroDead() {
        this.node.active = false;
        this.renderDead();
    }
    setData(data: PalBattleHero) {
        this.data = data;
    }

    render() {
        this.nameLabel.string = this.data.originData.name;
        const system = (GameManager.instance as GameManager).palBattleSystem;
        this.hpLabel.string = `${system.getHeroHp(this.data.saveData.UUID)}/${system.getHeroMaxHp(this.data.saveData.UUID)}`;
        this.renderDead();
        this.renderDefense();
    }
    renderDead() {
        if (this.data.saveData.hp <= 0) {
            this.hpLabel.string = 'DEAD';
        }
    }
    renderDefense() {
        this.defenseNode.active = this.data.saveData.defense;
    }
}


